<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\cms\controller;


use function PHPSTORM_META\type;
use think\admin\Controller;
use think\facade\Config;
use think\admin\service\AdminService;
use think\admin\extend\DataExtend;
use think\admin\service\MenuService;




/**
 * 复检管理
 * Class Educertcheck
 * @package app\admin\controller
 */
class Educertcheck extends BaseController
{

    /**
     * 当前操作数据库
     * @var string
     */
    protected $table = 'CmsEduCertCheck';
    static $PASS_STATE_OPTION = array(0=>"待考核",1=>"合格",2=>"不合格");

    /**
     * 复检管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {

        $this->title = "复检管理";
        $map = [];
        $query = $this->_query($this->table)->like('name,company')->where($map);

        $this->time_span = $this->request->get("time_span");
        if($this->time_span){
            $time_span = explode(" - ", $this->time_span);
            $query = $query->whereBetween("create_at",[ strtotime($time_span[0]),strtotime($time_span[1]) ]);
        }
        $query->order('id desc')->page();
    }

    protected function _getClassDict($class_ids){
        $map = array("id"=> $class_ids);
        $class = $this->app->db->name('CmsEduClass')->field("id,name")->where($map)->select();
        $result = array();
        for ($i=0; $i<count($class); $i++){
            $result[$class[$i]['id']] = $class[$i]['name'];
        }
        return $result;
    }

    protected function _getDataIds($data, $key){
        $arr = [];
        for ($i=0; $i<count($data); $i++){
            array_push($arr, $data[$i][$key]);
        }
        return $arr;
    }

    /**
     * 表单数据处理
     * @param array $vo
     * @throws \ReflectionException
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            // 清理权限节点
            AdminService::instance()->clearCache();
            if($vo && $vo['class_id'])
                $map = array("id"=>$vo['class_id']);
            else
                $map = array();
            if($vo){
                $this->enroll = $vo;
            }else{
                $this->enroll = array("cert_no"=>"","name"=>"","idno"=>"","gender"=>"男","class_name"=>"","memo"=>"",
                    "company"=>"","class_id"=>"","position_id"=>"","work_name"=>"","cert_date"=>"","cert_org"=>"");
            }

            $this->classes = $this->_get_class_enroll();
            for($i=0; $i<count($this->classes); $i++){
                $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
            }
            $this->classesJson = json_encode($this->classes);
            /*
            $this->class = $this->app->db->name("CmsEduClass")->where($map)->find();
            if($this->class){
                $this->work = $this->_get_work_ids($this->class['position_ids']);
                $this->workJson = json_encode($this->work);
            }else{
                $this->work = $this->_get_work_ids("");
                $this->workJson = json_encode($this->work);
            }*/
        }
    }

    //查询所有正在接受报名的开班信息
    public function _get_class_enroll(){
        $map = array("status"=>1);
        return $this->app->db->name('CmsEduClass')->field("id,name,position_ids")->where($map)
            ->whereBetween("create_at",[ time()-6*30*24*3600, time() ])
            ->select()->toArray();
    }

    public function _get_work_ids($position_ids){
        $work_ids = [];
        $position = $this->app->db->name('CmsEduWork')->alias("wp")
            ->field("wp.*,ew.name as class_name, ew.id as class_id")
            ->join('cms_edu_work ew', "ew.id=wp.parent_id")
            ->whereIn("wp.id", $position_ids)
            ->order('wp.id desc')->select()->toArray();

        for($i=0; $i<count($position); $i++){
            if(array_key_exists($position[$i]['class_id'], $work_ids)==false){
                $work_ids[ $position[$i]['class_id'] ] = array(
                    "id" => $position[$i]['class_id'],
                    "name" => $position[$i]['class_name'],
                    "sub" => [],
                );
            }
        }

        for($i=0; $i<count($position); $i++){
            $work_ids[ $position[$i]['class_id'] ]['sub'][] = $position[$i];
        }
        return array_values($work_ids);
    }

    /**
     * 删除复检记录
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    /**
     * 导出复检记录
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function export(){
        $title = [array("cert_no","证书编号"), array("name","学员姓名"),
                    array("gender","性别	"),array("idno","身份证"),array("company","单位名称"),
                    array("work_name","岗位类别"), array("position_name","工种"),
                    array("create_at","复检日期"), array("create_by","复检人员")];
        $data = $this->prepareExportData();
        $this->exportExcel($title, $data);

    }

    private function prepareExportData(){

        $map = [];
        $query= $this->app->db->name('CmsEduCertCheck');

        $this->name = $this->request->get("name");
        if($this->name) $query = $query->whereLike("ee.name","%".$this->name."%");
        $this->company = $this->request->get("company");
        if($this->company) $query = $query->whereLike("ee.company","%".$this->company."%");

        $this->time_span = $this->request->get("time_span");
        if($this->time_span){
            $timeSpan = explode(" - ", $this->time_span);
            $query = $query->whereBetween("create_at",[ strtotime($timeSpan[0]),strtotime($timeSpan[1]) ]);
        }
        $res = $query ->alias("ee")
            ->field("ee.*")
            ->where($map)
            ->order('ee.id desc')
            ->select()->toArray();
        for($i=0; $i<count($res); $i++){
            $res[$i]['create_at'] = date("Y-m-d", $res[$i]['create_at']);
        }
        return $res;
    }

}
